#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Program:
#   remove shell script for master uninstall
# Author:
#   Waue, Shunfa, Rock {waue, shunfa, rock}@nchc.org.tw
# Version:
#    1.0
# History:
#   

# [ Env para ] #
Crawlzilla_HOME="/home/crawler/crawlzilla"
Manu_Tmp_Path="/home/crawler/crawlzilla/.menu_tmp"
Hadoop_Daemon="/opt/crawlzilla/nutch/bin/hadoop-daemon.sh"
PID_Dir="/var/log/crawlzilla/hadoop-pids"
User_Del_Chk="y"

# [ System Load] #
## check user
#[ "`id -u`" != "0" ] && echo "please enter password of $USER:"  && exec sudo su -c "$0" "$@"
## language load 
lang=$(locale | grep 'LANGUAGE=' | cut -d "=" -f2 | cut -d ":" -f1 )
if [ "$lang" == "zh_TW" ] || [ "$lang" == "zh_TW.utf8" ] || \
   [ "$lang" == "zh_TW:zh" ];then
    source $Crawlzilla_HOME/system/lang/lang_zh_TW
else
    source $Crawlzilla_HOME/system/lang/lang_en_US
fi


#  [ Function Description ] #

function error_info ( )
{ 
    echo -e "\033[1;35;40m ERROR: $1 \033[0m" 
}
function show_info ( ) 
{
    echo -e "\033[1;32;40m $1 \033[0m"
}

# check root
function check_root ( )
{
  [ "`id -u`" != "0" ] && echo "please enter password of $USER:"  && exec sudo su -c "$0" "$@"
  if [ $USER != "root" ]; then
    show_info "$MR_check_root_1" # "請切換成 root 身份執行移除程式!!!"
    exit
  fi
  show_info "$MR_check_root_2" # "已確認為root身份, 將繼續執行此移除程式！"
}


#function for fast_shutdown_all computing nodes and tomcat server
function fast_shutdown_all ( )
{
  # shutdown tomcat
    show_info "$tomcat_switch_echo_4"
    tom_pids=$(ps ax | grep -v 'grep' | grep "tomcat" | awk '{print $1}')
    for tom_pid in $tom_pids
    do
        kill -9 $tom_pid 2>/dev/null
    done
  # kill hadoop
  if [ -e $PID_Dir/hadoop-crawler-datanode.pid ] || \
   [ -e $PID_Dir/hadoop-crawler-tasktracker.pid ] || \
   [ -e $PID_Dir/hadoop-crawler-namenodenode.pid ] || \
   [ -e $PID_Dir/hadoop-crawler-jobtracke.pid ];then
    kill -9 $(cat $PID_Dir/hadoop-crawler-datanode.pid) > /dev/null 2>&1 
    if [ ! $? -eq 0 ];then echo "datanode error !";fi
    kill -9 $(cat $PID_Dir/hadoop-crawler-tasktracker.pid) > /dev/null 2>&1
    if [ ! $? -eq 0 ];then echo "tasktracker error !";fi
    kill -9 $(cat $PID_Dir/hadoop-crawler-namenode.pid) > /dev/null 2>&1
    if [ ! $? -eq 0 ];then echo "namenode error !";fi
    kill -9 $(cat $PID_Dir/hadoop-crawler-jobtracker.pid) > /dev/null 2>&1
    if [ ! $? -eq 0 ];then echo "jobtracker error !";fi
  else 
   echo "ERROR ! cannot find $PID_Dir/*pid file"
   exit 8
  fi
}

# 移除檔案及資料夾
function remove_folders () {
  show_info "$MR_remove_folders_echo_1" # "正在刪除安裝時所建立的檔案及資料夾..."
  rm /usr/bin/crawlzilla
  rm /usr/bin/crawlzilla_remove
  rm -rf /opt/crawlzilla
  rm -rf /var/log/crawlzilla
  rm -rf /var/lib/crawlzilla
  
  show_info "$MR_remove_folders_echo_2" # "安裝時所建立的檔案及資料夾已刪除"
}

# recover /etc/hosts
function recover_hosts () {
  show_info "$CR_edit_hosts_echo_1" # "修改/etc/hosts..."
  if [ -e /home/crawler/crawlzilla/system/hosts.bak ];then
    rm /etc/hosts
    cp /home/crawler/crawlzilla/system/hosts.bak /etc/hosts
    show_info "$CR_edit_hosts_echo_2" # "完成修改/etc/hosts"
  elif [ -e /home/crawler/crawlzilla/system/hosts ];then
    rm /etc/hosts
    cp /home/crawler/crawlzilla/system/hosts /etc/hosts
    show_info " $CR_edit_hosts_echo_3"
  elif [ -f /etc/hosts ];then
    show_info " $CR_edit_hosts_echo_3"
  else
    show_info " BUG ! /etc/hosts error !!!"
  fi
}

# remove user
function user_delete () {
  show_info "$MR_user_delete_echo_1" # "正在刪除crawler使用者..."
  userdel -rf crawler >/dev/null 2>&1
  groupdel crawler >/dev/null 2>&1
  show_info "$MR_user_delete_echo_2" # "使用者crawler已刪除"
}

# remove crawlzilla init.d script
remove_crawlzilla_to_initd() {
linux_dist=$(lsb_release -i | awk '{print $3}')
# for debian system
if [ "$linux_dist" = "Ubuntu" ] || [ "$linux_dist" = "Debian" ]; then
    update-rc.d -f  crawlzilla remove
    rm /etc/init.d/crawlzilla
    # for rpm system
elif [ "$linux_dist" = "CentOS" ] || [ "$linux_dist" = "Fedora" ] || [ "$linux_dist" = "SUSE" ]; then
    chkconfig --del crawlzilla
    rm /etc/init.d/crawlzilla
else 
    show_info "Please delete /etc/init.d/ crawlzilla file manually."
fi
}

# Main function
function main () {
  show_info "$MR_main_echo_1" # a warnning hint
  # confirm 
  LOCK=0
  show_info "$MR_main_deluser_1 "
  while [ "$LOCK" == "0" ];
  do
    show_info "$MR_main_deluser_2"
    read -p "[y/n]: " User_Del_Chk
    show_info "$MR_main_echo_2" # a remove hint 
    if [ "$User_Del_Chk" == "y" ];then
      show_info "$MR_main_deluser_3" # show user delete selection
    else
      show_info "$MR_main_deluser_4" # show user delete selection
    fi
    read -p "$MR_main_echo_3" confirm # confirm 1.確定 2.取消"
  # make sure
    if [ $confirm -eq 1 ]; then
      fast_shutdown_all
      show_info "shutdown service"
      remove_folders
      show_info "remove_folders"
      recover_hosts
      show_info "recover hosts"
      remove_crawlzilla_to_initd
      if [ "$User_Del_Chk" == "y" ]; then
	user_delete
	show_info "user_delete"
      fi
      show_info "$MR_main_echo_4" # "移除程序已完成！"
      LOCK=1; 
    elif [ $confirm -eq 2 ]; then
      show_info "$MR_main_echo_5" # "您已取消移除程序！"
      show_info "$MR_main_echo_6" # "若要移除請再重新執行！"
      LOCK=1; 
    else
      show_info "Hint: 1 => yes, 2 => exit "
      show_info "Please enter 1 or 2"
    fi
  done
}

# [ Process Go ]

check_root
if [ ! -e "/home/crawler/crawlzilla" ] || [ ! -e "/opt/crawlzilla" ] ; then
  show_info "$MR_remove_check_1"
  show_info "$MR_remove_check_2"
  read -p "[y/n]: " force_do_remove
  if [ ! "$force_do_remove" == "y" ];then
    exit 0;
  fi
fi
main

